package com.example.common.result;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * @author YanMing
 */
@Data
public class ResponseResult<T> implements Serializable {

    /**
     * 操作状态码，rest风格
     */
    private Integer code;
    /**
     * 操作结果详细信息
     */
    private String message;
    /**
     * 实体类数据
     */
    private T result;


    public ResponseResult( int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseResult( int code, String message,T result) {

        this.code = code;
        this.message = message;
        this.result = result;
    }

    public ResponseResult(ResponseCode rc) {
        this.code = rc.code;
        this.message = rc.message;
    }

    public ResponseResult(ResponseCode rc, T result) {
        this.code = rc.code;
        this.message = rc.message;
        this.result = result;
    }


    public static ResponseResult success() {
        return new ResponseResult(ResponseCode.SUCCESS);
    }
    public static ResponseResult success(Object  result) {
        return new ResponseResult(ResponseCode.SUCCESS,result);
    }
    public static ResponseResult fail() {
        return new ResponseResult(ResponseCode.FAIL);
    }
    public static ResponseResult fail( String message) {
        ResponseResult result =  new ResponseResult(ResponseCode.FAIL);
        if(!StringUtils.isBlank(message)){
            result.setMessage(message);
        }
        return result;
    }

}
